//
//  NSString+lvfjHelper.m
//  lvfj
//
//  Created by 吕飞俊 on 15/11/25.
//  Copyright © 2015年 Goudu KuaiEx Technology(Shenzhen)Co.,ltd. All rights reserved.
//   _          ______   _      _
//  | |        |  ____| (_)    | |
//  | |  __   _| |__ ___ _     | |_   _ _ __
//  | |  \ \ / /  __/ _ \ |_   | | | | | '_ \
//  | |___\ V /| | |  __/ | |__| | |_| | | | |
//  |______\_/ |_|  \___|_|\____/ \__,_|_| |_|
//

#import "NSString+lvfjHelper.h"

@implementation NSString (lvfjHelper)


#pragma mark 去除空格
//首位
+(instancetype)cutLeadTailSpace:(NSString *)str{
    return [str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] ;
}

-(instancetype)cutLeadTailSpace{
    return [NSString cutLeadTailSpace:self] ;
}

+(instancetype)cutOtherString:(NSString *)string {
    NSCharacterSet *set = [NSCharacterSet characterSetWithCharactersInString:@"@／：；（）¥「」＂、[]{}#%-*+=_//|~＜＞$€^•'@#$%^&*()_+'/"""];
    return [string stringByTrimmingCharactersInSet:set];
}

//2.去掉多余的空格
-(instancetype)cutOutsideSpace{
    NSString *str = @"    this     is a    test    .   ";
    NSCharacterSet *whitespaces = [NSCharacterSet whitespaceCharacterSet];
    NSPredicate *noEmptyStrings = [NSPredicate predicateWithFormat:@"SELF != ''"];
      
    NSArray *parts = [str componentsSeparatedByCharactersInSet:whitespaces];
    NSArray *filteredArray = [parts filteredArrayUsingPredicate:noEmptyStrings];
    str = [filteredArray componentsJoinedByString:@" "];
    return str;
}

//去掉所有空格
-(instancetype)replaceSpace{
    return [self stringByReplacingOccurrencesOfString:@" " withString:@""];
}

//去掉最左边的空格
- (NSString *)stringByTrimmingLeftCharactersInSet:(NSCharacterSet *)characterSet {
    NSUInteger location = 0;
    NSUInteger length = [self length];
    unichar charBuffer[length];
    [self getCharacters:charBuffer];
  
    for (location; location < length; location++) {
        if (![characterSet characterIsMember:charBuffer[location]]) {
            break;
        }
    }
  
    return [self substringWithRange:NSMakeRange(location, length - location)];
}
//去掉最右边的空格
- (NSString *)stringByTrimmingRightCharactersInSet:(NSCharacterSet *)characterSet {
    NSUInteger location = 0;
    NSUInteger length = [self length];
    unichar charBuffer[length];
    [self getCharacters:charBuffer];
  
    for (length; length > 0; length--) {
        if (![characterSet characterIsMember:charBuffer[length - 1]]) {
            break;
        }
    }
  
    return [self substringWithRange:NSMakeRange(location, length - location)];
}

#pragma mark 空处理
/// 是否为空字符串
+ (BOOL)hq_isEmptyString:(NSString *)str
{
    if ([str isKindOfClass:[NSString class]]&&str.length>0) {
        NSCharacterSet *set = [NSCharacterSet whitespaceAndNewlineCharacterSet];
        return  [str stringByTrimmingCharactersInSet:set].length<1;
    }
    return YES;
}

+ (NSString *)hq_emptyStringReplayNil:(NSString *)str
{
    return [self hq_isEmptyString:str]?@"":str;
}
@end
